package p2pcdnsim.network;

import p2pcdnsim.exception.PortAlreadyInUseException;
import desmoj.core.simulator.Model;
import desmoj.core.simulator.SimTime;

public class ApplicationLayer {
	
	TransportLayer transport;
	Model owner;
	
	ApplicationProcess process;
	
	public ApplicationLayer(Model owner) {
		this.owner = owner;
	}

	public void setTransportLayer(TransportLayer transportLayer) {
		this.transport = transportLayer;
		
	}
	
	public void setApplicationProcess(ApplicationProcess app) {
		this.process = app;
		app.setApplicationLayer(this);
		app.activate(new SimTime(0.0));
	}

	public ServerSocket createServerSocket(ApplicationProcess applicationProcess, int port) throws PortAlreadyInUseException {
		return transport.createServerSocket(applicationProcess, port);
	}

	public Socket createSocket(ApplicationProcess applicationProcess,
			NetworkAddress address, int port) {
		return transport.createSocket(applicationProcess, address, port);
	}

	public ApplicationProcess getApplicationProcess() {
		return process;
	}

	public DatagramSocket createDatagramSocket(ApplicationProcess applicationProcess, int port) throws PortAlreadyInUseException {
		return transport.createDatagramSocket(applicationProcess, port);
	}

	public DatagramSocket createDatagramSocket(ApplicationProcess applicationProcess) {
		return transport.createDatagramSocket(applicationProcess);
	}

	TransportLayer getTransportLayer() {
		return transport;
	}
}
